package com.swn.common.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * <p>
 * Title: ServiceLocator.java
 * </p>
 * <p>
 * Description: 
 * </p>
 * @author 
 * @version 1.0
 * @created 
 */
public final class ServiceLocator {
	private static ApplicationContext context = null;
	
	/**
	 * @return 返回 context。
	 */
	protected static ApplicationContext getContext() {
		return ServiceLocator.context;
	}

	/**
	 * @param context 要修改的 context。
	 */
	protected static void setContext(ApplicationContext context) {
		ServiceLocator.context = context;
	}	
	
	/**
	 * Creates a new ServiceLocator object.
	 */
	private ServiceLocator() {
	}

	/**
	 * get manager service
	 * 
	 * @param service
	 *          service name
	 * 
	 * @return manager service
	 */
	public static Object getBean(final String service) {
		checkContext();
		return context.getBean(service);
	}
	
	  /**
     * 初始化spring应用上下文
     */
    private synchronized static void checkContext()
    {
    	if (context == null) {
            context = new ClassPathXmlApplicationContext("classpath:spring.xml");
        }
    }
}
